package com.mingshashan.building.easy.core.exception;

public interface EasyErrorCodeTranslator {
    public EasyErrorObject translate(ErrorCoded errorCoded, Object... paramVarArgs);

    public default EasyErrorObject translate(ErrorCoded errorCoded) {
        return translate(errorCoded, (Object[]) null);
    }

    public static final EasyErrorCodeTranslator DEFAULT = new EasyErrorCodeTranslator() {
        @Override
        public EasyErrorObject translate(ErrorCoded errorCoded, Object... params) {
            int status = errorCoded.getStatus();
            String code = errorCoded.getCode();
            String error = errorCoded.getErrorName();
            String message = EasyErrorCodeMessages.getMessage(errorCoded, params);
            int idx = message.indexOf("::");
            if (idx > 0) {
                error = message.substring(0, idx);
                message = message.substring(idx + "::".length());
            }
            EasyErrorObject errorObject = new EasyErrorObject(status, code, error, message);
            return errorObject;
        }
    };
}
